package problems.readerswriters;

import problems.BenchmarkSemaphore;
import helpers.IAction;


public class ReaderWriterWithDataRace implements IReaderWriter {
    private final LightSwitch _readerSwitch;
    private final LightSwitch _writerSwitch;
    private final BenchmarkSemaphore _roomEmpty;

    public ReaderWriterWithDataRace()
    {
        _readerSwitch = new LightSwitch();
        _writerSwitch = new LightSwitch();
        _roomEmpty = new BenchmarkSemaphore(1);
    }

    public void write(IAction action)
    {
        _writerSwitch.lock(_roomEmpty);
        action.invoke();
        _writerSwitch.unlock(_roomEmpty);
    }

    public void read(IAction action)
    {
        _readerSwitch.lock(_roomEmpty);
        action.invoke();
        _readerSwitch.unlock(_roomEmpty);
    }
}
